package com.gdit.zax.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.CollectDao;
import com.gdit.zax.dao.VideoDao;
import com.gdit.zax.entitys.CourseCollect;
import com.gdit.zax.entitys.VideoALL;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.MD5;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;

public class GetVideoList extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int courseid = TextUtil.String2int(request.getParameter("courseid"), -1);
		
		//检测参数
		if (courseid<0) {
			O.printFail(response, 1, "请检查courseid参数!");
			return;
		}
		
		//检测连接、用户
		Connection conn = DBUtil.getConnection();
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}
		
		//开始
		DAOFactory dao  = DAOFactory.init(conn, request, response);
		VideoDao vdao = dao.getVideoDao();
		ArrayList<VideoALL> list = vdao.listAllVideobycourse(courseid, 1, 1000);
		ArrayList<Map<String,Object>> outlist =  new ArrayList<Map<String,Object>>();
		for (VideoALL videoinfo : list) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("imageurl",Constant.WEBRoot + videoinfo.getT_course_pic());
			map.put("course", videoinfo.getT_course_name());
			map.put("section", videoinfo.getT_section_name());
			map.put("name", videoinfo.getT_video_name());
			map.put("anthor", videoinfo.getT_user_username());
			map.put("time", videoinfo.getT_video_addtime());
			map.put("url", Constant.WEBRoot +videoinfo.getT_video_url());
			map.put("pic",Constant.WEBRoot + videoinfo.getT_video_url());
			map.put("score", 0);
			map.put("commentsum", videoinfo.getT_video_commentsum());
			map.put("collectsum", videoinfo.getT_video_collectsum());//课程收藏数量
			map.put("downsum", videoinfo.getT_video_downsum());
			map.put("introduction", videoinfo.getT_video_introduction());
			
			outlist.add(map);
		}
		O.printSuccess(response, outlist);
	}

}
